package com.xms.form.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.dependency.XmsDependencyException;
import com.xms.form.entity.SystemForm;
import com.xms.schema.entity.Entity;
import com.xms.schema.event.EntityMetaDeletedEvent;
import com.xms.utils.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 删除实体后，级联删除表单
 * </p>
 *
 * @author migo
 * @since 2021/10/26
 */
@Service
public class SystemFormCascadeDeleter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private ISystemFormDeleterService _systemFormDeleterService;

    @Autowired
    public void SystemFormCascadeDeleter(ISystemFormDeleterService systemFormDeleterService) {
        _systemFormDeleterService = systemFormDeleterService;
    }

    @Async
    @EventListener
    public void onApplicationEvent(EntityMetaDeletedEvent event) throws SQLException, XmsDependencyException {
        logger.info("SystemFormCascadeDeleting");
        _systemFormDeleterService.deleteMany(new LambdaQueryWrapper<SystemForm>().eq(SystemForm::getEntityid, event.source.getEntityid()));
    }
}
